//
//  LocalCell.m
//  SXHomeTownClub
//
//  Created by 徐晨亮 on 2018/5/29.
//  Copyright © 2018年 jsx. All rights reserved.
//

#import "LocalCell.h"

@interface LocalCell () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIImageView *headerPic;
@property (nonatomic, strong) UILabel *nicknameLabel;
@property (nonatomic, strong) UILabel *levelLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UICollectionView *imagesCollectionView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIButton *funcButton;
@property (nonatomic, strong) UITableView *commentTableView;

@end

@implementation LocalCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self initializeSubviews];
        [self addConstraintByMasonry];
    }
    return self;
}

- (void)initializeSubviews {
    self.headerPic = [UIImageView new];
    [self.contentView addSubview:self.headerPic];
    
    self.nicknameLabel = [UILabel new];
    [self.contentView addSubview:self.nicknameLabel];
    
    self.levelLabel = [UILabel new];
    self.levelLabel.backgroundColor = SDColor(134, 173, 237);
    self.levelLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:self.levelLabel];
    
    self.contentLabel = [UILabel new];
    self.contentLabel.numberOfLines = 0;
//    self.contentLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.contentLabel];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.itemSize = CGSizeMake((kScreenWidth -90)/3, (kScreenWidth -90)/3);
    flowLayout.minimumLineSpacing = 3;
    flowLayout.minimumInteritemSpacing = 3;
    flowLayout.sectionInset = UIEdgeInsetsZero;
    self.imagesCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 1, 1) collectionViewLayout:flowLayout];
    self.imagesCollectionView.backgroundColor = [UIColor orangeColor];
    self.imagesCollectionView.delegate = self;
    self.imagesCollectionView.dataSource = self;
    [self.contentView addSubview:self.imagesCollectionView];
    [self.imagesCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"imagesCell"];
    
    self.timeLabel = [UILabel new];
    self.timeLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.timeLabel];
    
    self.funcButton = [UIButton new];
    [self.funcButton setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
    [self.contentView addSubview:self.funcButton];
    
    self.commentTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.commentTableView.tableFooterView = [UIView new];
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    [self.contentView addSubview:self.commentTableView];
    [self.commentTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"commentCell"];
}

- (void)addConstraintByMasonry {
    __weak typeof(self) weakSelf = self;
    [self.headerPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(16);
        make.size.mas_equalTo(50);
    }];
    [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.headerPic);
        make.left.mas_equalTo(weakSelf.headerPic.mas_right).offset(8);
    }];
    [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.nicknameLabel);
        make.left.mas_equalTo(weakSelf.nicknameLabel.mas_right).offset(8);
        make.right.mas_lessThanOrEqualTo(-16);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.headerPic.mas_bottom).offset(0);
        make.left.mas_equalTo(weakSelf.nicknameLabel);
//        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(kScreenWidth - 90);
    }];
    [self.imagesCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.contentLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(weakSelf.contentLabel);
//        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(kScreenWidth - 90);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.imagesCollectionView.mas_bottom).offset(8);
        make.left.mas_equalTo(weakSelf.imagesCollectionView);
    }];
    [self.funcButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.imagesCollectionView.mas_bottom).offset(8);
        make.right.mas_equalTo(weakSelf.imagesCollectionView);
    }];
    [self.commentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.timeLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(weakSelf.timeLabel);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark - UICollectionViewDelegate, UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.localModel.pic.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((kScreenWidth -90)/3, (kScreenWidth -90)/3);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imagesCell" forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.backgroundColor = [UIColor redColor];
    cell.backgroundView = imageView;
    NSString *imageString = self.localModel.pic[indexPath.row];
    [imageView sd_setImageWithURL:[NSURL URLWithString:imageString] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Group]];
    return cell;
}

#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.localModel.pllist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell" forIndexPath:indexPath];
    
    return cell;
}

- (void)setLocalModel:(LocalModel *)localModel {
    [self layoutIfNeeded];
    
    [self.headerPic sd_setImageWithURL:[NSURL URLWithString:localModel.headimg] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
    self.headerPic.layer.cornerRadius = 25;
    self.headerPic.layer.masksToBounds = YES;
    
    self.nicknameLabel.text = localModel.author;
    self.levelLabel.text = [NSString stringWithFormat:@"LV.%ld", localModel.level];
    self.contentLabel.text = localModel.subject;
    
    
    CGFloat collectionViewHeight = 0;
    CGFloat collectionViewWidth = kScreenWidth - 90;
    if (localModel.pic.count <= 3 && localModel.pic.count > 0) {
        collectionViewHeight = collectionViewWidth / 3;
    } else if (localModel.pic.count <= 6 && localModel.pic.count > 3) {
        collectionViewHeight = collectionViewWidth / 3 * 2;
    } else if (localModel.pic.count > 6 && localModel.pic.count <= 9) {
        collectionViewHeight = collectionViewWidth;
    }
    [self.imagesCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(collectionViewHeight);
    }];
//    [self.imagesCollectionView reloadData];
    
    self.timeLabel.text = localModel.date;
}

@end
